package com.chookss.mine.set

import android.os.Bundle
import com.chookss.R
import com.chookss.base.StaticClass
import com.chookss.base.Token
import com.chookss.login.LoginActivity
import com.chookss.mine.entity.AccountEntity
import com.chookss.tools.XJson
import com.chookss.tools.old.SignUtils
import com.chookss.video.ChangePassModel
import com.johnrambo.ktea.common.isOnlyNumbers
import com.johnrambo.ktea.ktExtends.*
import com.lvgroup.hospital.base.BaseAct
import errorToast
import infoToast
import kotlinx.android.synthetic.main.activity_change_pass.*
import kotlinx.android.synthetic.main.common_title.*
import java.util.*

/**
 * @ClassName:      修改密码页面
 * @Description:     java类作用描述
 * @CreateDate:     2021/4/15 16:31
 * @UpdateDate:     2021/4/15 16:31++
 * @UpdateRemark:   更新说明
 * @Version:        1.0
 */
class ChangePassActivity : BaseAct() {
    private val model by lazy { getViewModel<ChangePassModel>() }
    var type: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_change_pass)
        type = intent?.getStringExtra("type") ?: ""
        initState()
    }
    override fun initData() {
        common_title_txt.text="修改密码"
        common_title_back onclick {
            if (type == "login") {
                Token.quit()
                startActivity<LoginActivity>()
                finish()
            }else{
                finish()
            }
        }

        btn_new_pass_confirm onclick {

            val originalPass = ed_original_pass.text.toString()
            if (originalPass.isEmpty()) {
                errorToast("原始密码不能为空!")
                return@onclick
            }
            val newPass = ed_new_pass.text.toString()
            if (newPass.isEmpty()) {
                errorToast("新密码不能为空!")
                return@onclick
            }
            if (newPass.isOnlyNumbers()) {
                errorToast("密码不能为全数字,必须包含一个大写或小写字母")
                return@onclick
            }
            val newPassConfirm = ed_new_pass_confirm.text.toString()
            if (newPassConfirm.isEmpty()) {
                errorToast("请再次输入新密码!")
                return@onclick
            }
            if (newPassConfirm != newPass) {
                errorToast("两次密码不一致，请确认再试!")
                return@onclick
            }

            showLoading()

            val params: MutableMap<String, Any> = HashMap()
            params["oldPwd"] = originalPass
            params["onePwd"] = newPass
            params["twoPwd"] = newPassConfirm
            val timeStamp = SignUtils.getTimeStamp() //20201208125238
            val nonce = SignUtils.getRandomString(14)
            val makeSignStr = SignUtils.makeSignStr(params, timeStamp, nonce, true)

            model.changePassWord(
                originalPass,
                newPass,
                newPassConfirm,
                timeStamp,
                nonce,
                makeSignStr
            )
        }
        model.changePassLiveData.observe(this) {
            dismissLoading()
            if (it.getInt("code") == 200) {
                //val msg = it.getString("msg")
                infoToast("修改密码成功")
                var phone:String= ShareUtils().getString(this, StaticClass.APPACCOUNT, "")
                var companyName:String= ShareUtils().getString(this, StaticClass.COMPANYNAME, "")
                val list = XJson.getAccountsList(this@ChangePassActivity, phone, AccountEntity::class.java)
                for (i in list.indices) {
                    if (list[i].getCompanyName() == companyName) {
                        list[i].setPassword(ed_new_pass.text.toString())
                        XJson.setAccountsList(this@ChangePassActivity, phone, list)
                    }
                }
                if (type == "login") {
                    startActivity<LoginActivity>()
                }
                finish()

            } else {
                infoToast(it.getString("msg"))
            }
        }
        model.errorLiveData.observe(this) {
            dismissLoading()
            errorToast(it)
        }

    }

    override fun onBackPressed() {
        if (type == "login") {
            Token.quit()
            startActivity<LoginActivity>()
            finish()
        }else{
            finish()
        }
    }
}
























